package com.example.robotwatcher;

import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.example.robotwatcher.backend.DataCollector;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MineFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MineFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private DataCollector collector;
    private static Handler mhandler;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MineFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MineFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MineFragment newInstance(String param1, String param2) {
        MineFragment fragment = new MineFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        TextView PCname = view.findViewById(R.id.textView4);
        PCname.setText("PCname");
        EditText PCname1 = view.findViewById(R.id.Edittext18);
        PCname1.setText("");
        TextView IP = view.findViewById(R.id.textView5);
        IP.setText("IP");
        EditText IP1 = view.findViewById(R.id.Edittext19);
        IP1.setText("");
        TextView Instruction = view.findViewById(R.id.textView6);
        Instruction.setText("使用说明");
        EditText Instruction1 = view.findViewById(R.id.Edittext20);
        Instruction1.setText("");
        TextView HisPC = view.findViewById(R.id.textView7);
        HisPC.setText("历史设备列表");
        EditText HisPC1 = view.findViewById(R.id.Edittext21);
        HisPC1.setText("");
        TextView Verdiary = view.findViewById(R.id.textView8);
        Verdiary.setText("版本更新日志");
        EditText Verdiary1 = view.findViewById(R.id.Edittext22);
        Verdiary1.setText("");
        TextView Verinfor = view.findViewById(R.id.textView9);
        Verinfor.setText("版本信息");
        EditText Verinfor1 = view.findViewById(R.id.Edittext23);
        Verinfor1.setText("");
        TextView About = view.findViewById(R.id.textView10);
        About.setText("关于");
        EditText About1 = view.findViewById(R.id.Edittext24);
        About1.setText("");

        mhandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message msg) {
                try {
                    JSONObject json = (JSONObject) msg.obj;
                    Verdiary.setText("软件版本:"+json.getString("version"));
                }catch (JSONException e) {
                    e.printStackTrace();
                }
                return false;
            }
        });

        SharedPreferences dataset = PreferenceManager.getDefaultSharedPreferences(inflater.getContext());
        collector = new DataCollector(mhandler);
        collector.setTargetUrl(dataset.getAll().get("defult").toString());
        collector.getDeviceInfo();

        return view;

    }
}